define(
    [
        'jquery', 
        'bootstrap', 
        'backend', 
        'addtabs', 
        'table', 
        'echarts', 
        'echarts-theme', 
        'template',
        'form'
    ], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template, Form) {

    var Controller = {
        fun: {
            echarts_data: function(){
                $.post(
                    'pms/pmsreport/index',
                    {
                        search_dates: $('#search_dates').val()
                    },
                    function(d){
                        // console.log(d);
                        $('#order_total').text(d['order_total']);
                        $('#occupancy_rate').text(d['occupancy_rate']);
                        $('#reserve_total').text(d['reserve_total']);
                        $('#gmv_total').text(d['gmv_total']);
                        $('#payment_total').text(d['payment_total']);

                        var echarts1_data = d['echarts1'];
                        var echarts2_data = d['echarts2'];
                        var echarts3_data = d['echarts3'];
                        var echarts4_data = d['echarts4'];
                        var echarts5_data = d['echarts5'];

                        Controller.fun.echarts1(echarts1_data);
                        Controller.fun.echarts2(echarts2_data);
                        Controller.fun.echarts3(echarts3_data);
                        Controller.fun.echarts4(echarts4_data);
                        Controller.fun.echarts5(echarts5_data);
                    }
                    ,'json'
                );
            },
            echarts1: function(echarts1_data){
                var myChart = Echarts.init(document.getElementById('echarts1'), 'walden');
                var option = {
                    title: {
                        text: '订单、预订单、销售额统计'
                    },
                    legend: {
                        data: echarts1_data['legend_data']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    xAxis: {
                        type: 'category',
                        data: echarts1_data['xAxis_data']
                    },
                    yAxis: [
                        {
                            type: 'value',
                            name: '订单、预订单',
                            interval: 10
                        },
                        {
                            type: 'value',
                            name: '销售额(元)',
                            interval: 5000,
                        },
                    ],
                    series: echarts1_data['series']
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

                $(window).resize(function () {
                    myChart.resize();
                });
            },
            echarts2: function(echarts2_data){
                var myChart = Echarts.init(document.getElementById('echarts2'), 'walden');
                var option = {
                    title: {
                        text: '房型入住统计'
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    xAxis: {
                        type: 'value'
                    },
                    yAxis: {
                        type: 'category',
                        data: echarts2_data['yAxis_data']
                    },
                    series: [{
                        data: echarts2_data['series_data'],
                        type: 'bar'
                    }]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

                $(window).resize(function () {
                    myChart.resize();
                });
            },
            echarts3: function(echarts3_data){
                var myChart = Echarts.init(document.getElementById('echarts3'), 'walden');
                var option = {
                    title: {
                        text: '入住客人年纪统计',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                    },
                    series: [
                        {
                            name: '人数',
                            type: 'pie',
                            radius: '50%',
                            data: echarts3_data,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            },
                        　　label : {
                        　　　　normal : {
                        　　　　　　formatter: '{b}:{c}: ({d}%)',
                        　　　　　　textStyle : {
                        　　　　　　　　fontWeight : 'normal',
                        　　　　　　　　fontSize : 15
                        　　　　　　}
                        　　　　}
                        　　},
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

                $(window).resize(function () {
                    myChart.resize();
                });
            },
            echarts4: function(echarts4_data){
                var myChart = Echarts.init(document.getElementById('echarts4'), 'walden');
                var option = {
                    title: {
                        text: '房间物品使用统计'
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    xAxis: {
                        type: 'value'
                    },
                    yAxis: {
                        type: 'category',
                        data: echarts4_data['yAxis_data']
                    },
                    series: [{
                        data: echarts4_data['series_data'],
                        type: 'bar',
                        color: '#91cc75'
                    }]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

                $(window).resize(function () {
                    myChart.resize();
                });
            },
            echarts5: function(echarts5_data){
                var myChart = Echarts.init(document.getElementById('echarts5'), 'walden');
                var option = {
                    title: {
                        text: '服务统计'
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    xAxis: {
                        type: 'value'
                    },
                    yAxis: {
                        type: 'category',
                        data: echarts5_data['yAxis_data']
                    },
                    series: [{
                        data: echarts5_data['series_data'],
                        type: 'bar',
                        color: '#fc8452'
                    }]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

                $(window).resize(function () {
                    myChart.resize();
                });
            },
        },
        index: function () {
            $('.search-btn').click(function(){
                var that = $(this);
                $('.search-btn').removeClass('btn-info').addClass('btn-link');
                that.addClass('btn-info').removeClass('btn-link');
                $("#search_dates").val(that.data('dates'));

                Controller.fun.echarts_data();
            });

            var search_dates_I = 1;
            $("#search_dates").data("callback", function (start, end) {
                if (search_dates_I % 2 == 1)
                {
                    date = start.format(this.locale.format) + " - " + end.format(this.locale.format);
                    $(this.element).val(date);
                    // console.log(date)

                    Controller.fun.echarts_data();
                }

                search_dates_I++;
            });
            

            Controller.fun.echarts_data();

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});